
layui.use(['laydate','table','form'],
    function() {
        let table = layui.table;
        let laydate = layui.laydate
        createSelect()

        createTable()

///执行一个laydate实例
        laydate.render({
             elem: '#start' //指定元素
         });

//执行一个laydate实例
           laydate.render({
               elem: '#end' //指定元素
           });
// crateTable();
        var form = layui.form;
        form.on('submit(sreach)',function (){
            createTable();
            return false;

        });
//监听工具条
        table.on('tool(news)', function(obj) {
            let data = obj.data;
            if (obj.event === 'detail') {
                sessionStorage.setItem("newsId", data.id)
                xadmin.open('查看', "/html/news/news-info.html", 600, 400)
            }
            else if(obj.event === 'home'){
                if(data.isHome=='是'){
                    layer.confirm('是否取消主页显示',function (index) {
                        $.ajax({
                            url: '/back/news/changeHomeState',//地址：访问后台的地址
                            data: {id: data.id,
                                loginName:sessionStorage.getItem("loginName")},//前端传递给后台的数据
                            type: 'post',
                            dataType: 'json',//是后端传递到前端的数据格式
                            success: function (data) {//后端给前端的一个反馈
                                if(data.result=="notLogin"){
                                    window.open("/html/login.html", "_top");
                                }
                                if (data.result == "success") {
                                    layer.msg('已改变!', {icon: 1, time: 1000});
                                    xadmin.close();
                                    xadmin.father_reload();
                                }
                            }
                        })
                        return false;
                    })
                }
                else{
                    layer.confirm('是否主页显示',function (index) {
                        $.ajax({
                            url: '/back/news/changeHomeState',//地址：访问后台的地址
                            data: {id: data.id,
                                loginName:sessionStorage.getItem("loginName")},//前端传递给后台的数据
                            type: 'post',
                            dataType: 'json',//是后端传递到前端的数据格式
                            success: function (data) {//后端给前端的一个反馈
                                if(data.result=="notLogin"){
                                    window.open("/html/login.html", "_top");
                                }
                                if (data.result == "success") {

                                    layer.msg('已改变!', {icon: 1, time: 1000});
                                    xadmin.close();
                                    xadmin.father_reload();

                                }
                            }
                        })
                        layer.close(index);
                        return false;
                    })
                }
            }
            else if (obj.event === 'del') {
                layer.confirm('真的删除行么', function (index) {

                    $.ajax({
                        url: '/back/news/delete',//地址：访问后台的地址
                        data: {id: data.id,
                            loginName:sessionStorage.getItem("loginName")},//前端传递给后台的数据
                        type: 'post',
                        dataType: 'json',//是后端传递到前端的数据格式
                        success: function (data) {//后端给前端的一个反馈
                            if(data.result=="notLogin"){
                                window.open("/html/login.html", "_top");
                            }
                            if (data.result == "success") {
                                $(obj).parents("tr").remove();
                                layer.msg('已删除!', {icon: 1, time: 1000});
                            }
                        }


                    });
                    obj.del();
                    layer.close(index);
                    return false;

                    //sessionStorage.setItem("lawyerId", data.id)
                });
            } else if (obj.event === 'edit') {
                sessionStorage.setItem("newsId", data.id)
                xadmin.open('编辑', "/html/news/news-edit.html", 600, 400)
                //layer.alert('编辑行：<br>' + JSON.stringify(data))
            }
        })
    });

function createTable() {

    layui.use(['layer','table'],function () {
        let layer = layui.layer;
        let table = layui.table;
        table.render(
            {
                elem:"#lawyer-list",
                url:"/back/news/list",
                where:{
                    titles:$("#title").val(),
                    type:$("#types").val(),
                    start:$("#start").val(),
                    end:$("#end").val(),
                    isHome:$("#home").val(),
                    loginName:sessionStorage.getItem("loginName")

                },
                page:true,
                parseData: function (res) {
                    if(res.result=="notLogin"){
                        window.open("/html/login.html", "_top");
                    }
                    console.log(res)
                    return{
                        "code":0,
                        "msg":"",
                        "total":res.data.length,
                        "data":res.data,
                        "count":res.count,
                    };
                },
                cols:[[
                    {field:'id',title:'ID',width:80,sort:true,fixed:'left'},
                    {field:'title',title:'标题',width:120},
                    {field:'count',title:'浏览量',width:100,sort:true},
                    {field:'dependenceObj',title:'类别',width:100},
                    {field:'isHome',title:'是否主页显示 ',width:100},
                    {field:'updateTime',title:'最后更新时间',width:200,sort:true},
                    {fixed: 'right', width:250, align:'center', toolbar: '#barDemo'}
                ]],

            }
        );






    })


    console.log(2)
}

function createSelect() {
    let select = $("#types")


    layui.use('form',function () {

        $.ajax({
            url:'/back/type/all',
            dataType:'json',
            type:'get',
            data:{loginName:sessionStorage.getItem("loginName")},
            success:function (res) {
                if(res.result=="notLogin"){
                    window.open("/html/login.html", "_top");
                }
                select.empty()
                select.append(new Option("全部","全部"))
                let data =res.types;
                $.each(data,function (index, item) {

                    select.append(new Option(item.type,item.type))
                    console.log(select)
                })

                layui.form.render("select");
            }
        })
    })
}



